Session 4: Quarto

Justin Lee

2024-04-12

Notes:

  • jl usually starts a Quarto doc with notes, to-dos, where i left off, etc
    • test

Data source

  • Data last downloaded on XXX
  • Box directory: /Box-Box/jlee/QSU_RWorkshop/data/baseball_gamelogs2023.rds

Table 1

Code
### USING GTSUMMARY ###
require(gtsummary)
## note on require -- does the same as library() but will continue executing submitted code if package does not exist; not really useful in this case, but can be useful if its within an Rscript or function where you still want the code to run

### try using gtsummary -- 
reset_gtsummary_theme()
# theme_gtsummary_journal(journal = "jama")
theme_gtsummary_compact()

dput(names(tab1))
c("Name", "Team", "GS", "G", "IP", "W", "L", "QS", "SV", "HLD", 
"H", "ER", "HR", "SO", "BB", "K.9", "BB.9", "K.BB", "HR.9", "AVG", 
"WHIP", "BABIP", "LOB.", "ERA", "FIP", "ADP", "division", "POS"
)
Code
vars_tab1 <- c("Team", "IP", "W", "L", "SV", "K.9", "BB.9", "K.BB", "ERA", "WHIP")

tab1 %>%
  dplyr::select(vars_tab1) %>%
  gtsummary::tbl_summary(by=Team,
    type = list(c("IP", "W", "L", "SV", "K.9", "BB.9", "K.BB", "ERA", "WHIP") ~ "continuous"),
              # statistic = list(c("ConnectivityZscore") ~ "{median} [{p25}, {p75}]"
              #                  ),
              missing_text = "Missing",
              label = list(IP ~ "Innings Pitched",
                           W ~ "Wins",
                           L ~ "Losses",
                           SV ~ "Saves",
                           K.9 ~ "Strikeouts per 9 innings",
                           BB.9 ~ "Walks per 9 innings",
                           K.BB ~ "Strikeouts per walk",
                           ERA ~ "Earned Run Average",
                           WHIP ~ "Walks + Hits per inning pitched"
                          )
              ) %>%
  add_stat_label() %>%
  modify_spanning_header(all_stat_cols() ~ "**Team**") %>%
  add_difference(everything() ~ "smd") %>%
  modify_column_hide(columns=ci)
Characteristic Team Difference1
LAD, N = 12 SFG, N = 6
Innings Pitched, Median (IQR) 112 (91, 123) 86 (66, 129) 0.07
Wins, Median (IQR) 8 (6, 10) 6 (4, 7) 0.43
Losses, Median (IQR) 5 (4, 6) 4 (4, 6) -0.13
Saves, Median (IQR) 0 (0, 1) 1 (0, 2) -0.29
Strikeouts per 9 innings, Median (IQR) 9.29 (8.72, 10.16) 9.38 (7.84, 11.14) 0.00
Walks per 9 innings, Median (IQR) 2.90 (2.52, 3.29) 3.18 (2.28, 3.90) -0.20
Strikeouts per walk, Median (IQR) 3.24 (2.67, 3.66) 3.09 (2.71, 3.41) 0.24
Earned Run Average, Median (IQR) 3.72 (3.57, 4.38) 3.90 (3.71, 3.97) 0.22
Walks + Hits per inning pitched, Median (IQR) 1.17 (1.15, 1.30) 1.27 (1.24, 1.33) -0.81
1 Standardized Mean Difference

Create separate tabs for different tables/figures/text

  • set up function for plots
    • jl steal what we did in session3_advancedR.qmd

Plot BA and OBP

Code
## now call the function with my parameters of choice:
plot.stat(data=df_gamelog_long, player_var = c("bettsmo"), stat_var = c("BA", "OBP"))

Plot R and RBI

  • fdsajiopgwjp
Code
## now call the function with my parameters of choice:
plot.stat(data=df_gamelog_long, player_var = c("bettsmo"), stat_var = c("R", "RBI"))

Plot BA and OBP

  • jl note direct label getting cut off, up the fig-width for this code chunk

Something other than the plot.stat function

  • turn this into a plotly

    • plotly::ggplotly()

try to get a Shiny plot to filter by any id? – in progress

  • jl this works in Viewer, but Shiny code needs to be hosted somewhere so won’t show up in the html output
  • in YAML need to set server: shiny

jl testing iframe to sneak in Shiny app into Quarto html file?

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.